package com.cauli.manage.operationLog.utils;

import cn.hutool.extra.servlet.ServletUtil;
import com.cauli.manage.operationLog.model.entity.OperationLog;
import com.qiwenshare.common.util.DateUtil;

import javax.servlet.http.HttpServletRequest;

public class OperationLogUtil {
    /**
     * 构造操作日志参数
     *
     * @param request   请求
     * @param isSuccess 操作是否成功
     * @param source    操作源模块
     * @param operation 执行操作
     * @param detail    详细信息
     * @return 操作日志参数
     */
    public static OperationLog getOperationLogObj(HttpServletRequest request, Long userId, String isSuccess, String source, String operation, String detail) {
        // 用户需要登录才能进行的操作，需要记录操作日志
        OperationLog operationLog = new OperationLog();
        operationLog.setUserId(userId);
        operationLog.setTime(DateUtil.getCurrentTime());
        operationLog.setTerminal(ServletUtil.getClientIP(request));
        operationLog.setSource(source);
        operationLog.setResult(isSuccess);
        operationLog.setOperation(operation);
        operationLog.setDetail(detail);
        operationLog.setRequestMethod(request.getMethod());
        operationLog.setRequestUrl(request.getRequestURI());

        if ("文件操作".equals(source) || "文件传输".equals(source) || "回收站".equals(source) || "文件分享".equals(source)) {
            operationLog.setPlatform(1);
        } else {
            operationLog.setPlatform(0);
        }
        return operationLog;
    }
}
